﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;

namespace Fitment.User
{
    public partial class register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                clear();
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            string acc = tb_acc.Text.ToString().Trim();
            string pwd = tb_pwd.Text.ToString().Trim();
            string name = tb_name.Text.ToString().Trim();
            string sex = dd_sex.Text.ToString().Trim();
            string age = dd_age.Text.ToString().Trim();
            string tel = tb_tel.Text.ToString().Trim();
            string email = tb_email.Text.ToString().Trim();
            string address = tb_add.Text.ToString().Trim();
            int iage = 0;
            if (age.Equals("请选择"))
            {
                iage = 0;
            }
            else
            {
                iage = Convert.ToInt32(age);
            }
            Userinfo user = new Userinfo(0, acc, pwd, name, sex,iage, tel, email, address);
            int result = user.save();
            if (result == 1)
            {
                sendEmail(email, acc, pwd);
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "提示", "<script>alert('注册成功，现在就去登录吧~');</script>");
                Response.Write("<script>setTimeout(\"location.href='../logon.aspx'\", 100);</script>");
                //Response.Redirect("../logon.aspx");
            }
            else if (result == 0)
            {
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "提示", "<script>alert('注册不成功，请重新操作！');</script>");
            }
            else if (result == 2)
            {
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "提示", "<script>alert('用户重复，请修改用户名！');</script>");
            }
            else { }
        }
        protected void clear()
        {
            tb_acc.Text = "";
            tb_pwd.Text = "";
            tb_pwd2.Text = "";
            tb_name.Text = "";
            dd_sex.Text = "男";
            dd_age.Text = "请选择";
            tb_tel.Text = "";
            tb_email.Text = "";
            tb_add.Text = "";
        }
        protected bool sendEmail(string email, string account, string pwd)
        {
            //设置发件人信箱,及显示名字 
            MailAddress from = new MailAddress("ibmcics2011@126.com", "装修预算指南系统管理员");
            //设置收件人信箱,及显示名字 
            MailAddress to = new MailAddress(email, account);
            //创建一个MailMessage对象 
            MailMessage oMail = new MailMessage(from, to);

            oMail.Subject = "关于装修预算指南系统的注册信息"; //邮件标题 
            oMail.Body = "尊敬的用户：您好，你的用户名是：" + account + ", 密码是：" + pwd + ",请保存邮件，方便以后找回密码。"; //邮件内容 

            oMail.IsBodyHtml = true; //指定邮件格式,支持HTML格式 
            oMail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");//邮件采用的编码 
            oMail.Priority = MailPriority.Normal;//设置邮件的优先级
            //发送邮件服务器 
            SmtpClient client = new SmtpClient();
            client.Host = "smtp.126.com"; //指定邮件服务器 
            client.Credentials = new System.Net.NetworkCredential("ibmcics2011@126.com", "111111");//指定服务器邮件,及密码 

            //发送 
            try
            {
                client.Send(oMail); //发送邮件 
                return true;
            }
            catch (Exception ex)
            {
                string error = ex.ToString();
                return false;
            } 
        }
    }
}
